//
//  FloatingPlayerModel.m
//  F FloatingPlayer
//
//  Created by yonas on 2018/9/26.
//  Copyright © 2018年 Inspur. All rights reserved.
//

#import "FloatingViewModel.h"



@implementation FloatingViewModel

@synthesize originalFrame = _originalFrame;

- (instancetype)init{
    if (self = [super init]){
        
    }
    return self;
}
- (void)setWillShowView:(UIView *)willShowView{
    _willShowView = willShowView;
    //保存view的原始frame
    _originalFrame = _willShowView.frame;
}

- (void)setWillShowLayer:(CALayer *)willShowLayer{
    _willShowLayer = willShowLayer;
    _originalFrame = _willShowLayer.frame;
}
- (CGRect)viewFrame{
    if (_positionOption == PositionOptionsDefault){
        return _viewFrame;
    }else{
        //1. 保证同一个方向只能有一个0-1的值。
        if ((_positionInsets.left>0&&_positionInsets.right>0)||((_positionInsets.left==0&&_positionInsets.right==0))){
            NSAssert(false, @"FloatingPlayer:每个方向的insets只能有一个值，请检查横向insets的值");
        }
        
        if ((_positionInsets.top>0&&_positionInsets.bottom>0)||((_positionInsets.top==0&&_positionInsets.bottom==0))){
            NSAssert(false, @"FloatingPlayer:每个方向的insets只能有一个值，请检查竖向insets的值");
        }
        
        if (_scale == 0){
            _scale = 1;
        }
        
        if (memcmp(&_positionInsets, &UIEdgeInsetsZero, sizeof(_positionInsets)) == 0){
            //默认右下角。
            _positionInsets = UIEdgeInsetsMake(0, 0, 1, 1);
        }
        //2.计算位置
        CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
        CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
        CGFloat _w = (_willShowView?_willShowView.frame.size.width:_willShowLayer.frame.size.width) * _scale;
        CGFloat _h = (_willShowView?_willShowView.frame.size.height:_willShowLayer.frame.size.height) * _scale;
        CGFloat _x = 0;
        //根据right和left计算x
        if (_positionInsets.right>0){
            _x = screenW - _w - screenW * (1 - _positionInsets.right);
        }else if (_positionInsets.left>0){
            _x = screenW * (1 - _positionInsets.left);
        }
        CGFloat _y = 0;
        if (_positionInsets.top>0){
            _y = 64 + screenH * (1 - _positionInsets.top);
        }else if (_positionInsets.bottom>0){
            _y = screenH - 49 - screenH * (1 - _positionInsets.bottom) - _h;
        }
        
        NSAssert(_x>=0&&_x+_w<=screenW&&_y>=0&&_y+_h<=screenH, @"FloatingPlayer:超出屏幕显示范围，请调整view大小");
        _viewFrame = CGRectMake(_x, _y, _w, _h);
        return _viewFrame;
    }
}
- (CGRect)windowFrame{
    return _viewFrame;
}
- (CGRect)originalFrame{
    return _originalFrame;
}
- (void)dealloc{
    NSLog(@"%s",__func__);
}
@end
